package gestion.camping.vue;

import gestion.camping.controleur.OccupantController;
import gestion.camping.controleur.PersonneController;
import gestion.camping.controleur.ReservationController;
import gestion.camping.controleur.VisiteurController;
import gestion.camping.dto.Reservation;
import gestion.camping.dto.TypeLocation;
import gestion.camping.utils.DateFormatUtils;
import gestion.camping.utils.MessagesUtil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class ViewReservationDetail extends JDialog implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Reservation reservation;
	private List<TypeLocation> listTypeLocation;
	private ReservationController controller;
	private PersonneController controllerPersonne;
	private OccupantController controllerOccupant;
	private VisiteurController controllerVisiteur;
	private JTextField textDateDepart;
	private JTextField textDateFin;
	private JComboBox comboBoxTypeLocation;
	private JTextField textTypeLocation;
	private JButton btnEnregistrer;
	private ViewPersonneListe panelPersonne;
	private JTextField textFraisResa;
	private JTextField textTotal;
	private JTextField textARRHES;
	private JButton btnCalculMontant;
	private JButton btnTestDispo;
	private JTextField textStatut;
	private JLabel lblStatut;
	private JTextField textField_Nom;
	private JTextField textField_prenom;
	private JTextField textField_ddn;
	private JTextField textField_adresse;
	private JTextField textField_codePostal;
	private JTextField textField_ville;
	private JLabel lblNewLabel_codePostal;
	private JLabel lblNewLabel_ville;
	private JTextField textField_telephone;
	private JTextField textField_emplacementNum;
	private JTextField textField_emplacementSecteur;
	private JTextField textField_emplacementTypeTerrain;
	private JLabel lblNewLabel_typeTerrain;
	private JTextField textField_carte;
	private JLabel lblNewLabel_Carte;
	private JTextField textField_badge;
	private JLabel lblNewLabel_badge;
	private JLabel lblNewLabel_secteur;
	private JLabel lblNewLabel_emplacementNum;
	private JLabel lblNewLabel_telephone;
	private JButton btnOccupant;
	private JButton btnVisiteur;
	private JButton btnAttributionCarte;
	private JButton btnRetourListe;
	private JButton btnFacture;
	private JPanel panel;
	private JPanel panelClient;
	private JPanel panelEmplacement;
	private JPanel panelCarte;
	private JPanel panelInfoResa;
	private JButton btnRestitutionCarte;
	
	/**
	 * Create the panel.
	 */
	public ViewReservationDetail(ReservationController controller) {
		this.controller = controller;
		
		controllerPersonne = new PersonneController(false);		
		this.reservation = controller.getReservation();
		
		initCompoment();
		
		//On initialise la liste des types de locations
		setListTypeLocation(controller.getListTypeLocation());
		
	}
		
	public void initCompoment(){
		
		setBounds(25, 25, 1150, 750);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{20, 0, 0, 0, 0, 0, 0, 20};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
		getContentPane().setLayout(gridBagLayout);
		
		btnRetourListe = new JButton(MessagesUtil.formatMessage("visiteur.ajout.btn.retour"));
		btnRetourListe.addActionListener(this);
		GridBagConstraints gbc_btnRetourListe = new GridBagConstraints();
		gbc_btnRetourListe.anchor = GridBagConstraints.EAST;
		gbc_btnRetourListe.insets = new Insets(0, 0, 5, 5);
		gbc_btnRetourListe.gridx = 6;
		gbc_btnRetourListe.gridy = 0;
		getContentPane().add(btnRetourListe, gbc_btnRetourListe);
		
		
		panelInfoResa = new JPanel();
		panelInfoResa.setBorder(new TitledBorder(null, MessagesUtil.formatMessage("reservation.detail.panel.info"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridwidth = 6;
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 1;
		getContentPane().add(panelInfoResa, gbc_panel_1);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0, 0, 0, 0, 20, 0};
		gbl_panel_1.rowHeights = new int[]{0, 0};
		gbl_panel_1.columnWeights = new double[]{0.15, 0.15, 0.15, 0.15, 0.15, 0.15, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panelInfoResa.setLayout(gbl_panel_1);
		
		JLabel lblTypeLocation = new JLabel(MessagesUtil.formatMessage("reservation.detail.typeLocation"));
		GridBagConstraints gbc_lblTypeLocation = new GridBagConstraints();
		gbc_lblTypeLocation.anchor = GridBagConstraints.EAST;
		gbc_lblTypeLocation.insets = new Insets(0, 0, 5, 5);
		gbc_lblTypeLocation.gridx = 0;
		gbc_lblTypeLocation.gridy = 0;
		panelInfoResa.add(lblTypeLocation, gbc_lblTypeLocation);
		
		if(reservation.getStatut().getId() == 0){
			comboBoxTypeLocation = new JComboBox();
			GridBagConstraints gbc_comboBoxTypeLocation = new GridBagConstraints();
			gbc_comboBoxTypeLocation.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxTypeLocation.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxTypeLocation.gridx = 1;
			gbc_comboBoxTypeLocation.gridy = 0;
			panelInfoResa.add(comboBoxTypeLocation, gbc_comboBoxTypeLocation);
		}else{
			textTypeLocation = new JTextField(reservation.getTypeLocation().getLibelle());
			textTypeLocation.setEditable(false);
			GridBagConstraints gbc_comboBoxTypeLocation = new GridBagConstraints();
			gbc_comboBoxTypeLocation.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxTypeLocation.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxTypeLocation.gridx = 1;
			gbc_comboBoxTypeLocation.gridy = 0;
			panelInfoResa.add(textTypeLocation, gbc_comboBoxTypeLocation);
		}
		
		JLabel lblDateDepart = new JLabel(MessagesUtil.formatMessage("reservation.detail.dateDepart"));
		GridBagConstraints gbc_lblDateDepart = new GridBagConstraints();
		gbc_lblDateDepart.anchor = GridBagConstraints.EAST;
		gbc_lblDateDepart.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateDepart.gridx = 2;
		gbc_lblDateDepart.gridy = 0;
		panelInfoResa.add(lblDateDepart, gbc_lblDateDepart);
		
		textDateDepart = new JTextField(reservation.getDateDebutString());
		if(reservation.getStatut().getId() >= 1){
			textDateDepart.setEditable(false);
		}
		GridBagConstraints gbc_textDateDepart = new GridBagConstraints();
		gbc_textDateDepart.insets = new Insets(0, 0, 5, 5);
		gbc_textDateDepart.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDateDepart.gridx = 3;
		gbc_textDateDepart.gridy = 0;
		panelInfoResa.add(textDateDepart, gbc_textDateDepart);
		textDateDepart.setColumns(10);
		
		JLabel lblDateFin = new JLabel(MessagesUtil.formatMessage("reservation.detail.dateFin"));
		GridBagConstraints gbc_lblDateFin = new GridBagConstraints();
		gbc_lblDateFin.anchor = GridBagConstraints.EAST;
		gbc_lblDateFin.insets = new Insets(0, 0, 5, 5);
		gbc_lblDateFin.gridx = 4;
		gbc_lblDateFin.gridy = 0;
		panelInfoResa.add(lblDateFin, gbc_lblDateFin);
		
		textDateFin = new JTextField(reservation.getDateFinString());
		if(reservation.getStatut().getId() >= 1){
			textDateFin.setEditable(false);
		}
		GridBagConstraints gbc_textDateFin = new GridBagConstraints();
		gbc_textDateFin.insets = new Insets(0, 0, 5, 0);
		gbc_textDateFin.fill = GridBagConstraints.HORIZONTAL;
		gbc_textDateFin.gridx = 5;
		gbc_textDateFin.gridy = 0;
		panelInfoResa.add(textDateFin, gbc_textDateFin);
		textDateFin.setColumns(10);
		
		btnTestDispo = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.testDispo"));
		btnTestDispo.addActionListener(this);
		if(reservation.getStatut().getId() >= 1){
			btnTestDispo.setEnabled(false);
		}
		GridBagConstraints gbc_btnTestDispo = new GridBagConstraints();
		gbc_btnTestDispo.anchor = GridBagConstraints.EAST;
		gbc_btnTestDispo.insets = new Insets(0, 0, 5, 0);
		gbc_btnTestDispo.gridx = 5;
		gbc_btnTestDispo.gridy = 1;
		panelInfoResa.add(btnTestDispo, gbc_btnTestDispo);
		
		if(reservation.getStatut().getId() == 0){
			panelPersonne = controllerPersonne.getViewListe();
			panelPersonne.setBorder(new TitledBorder(null, MessagesUtil.formatMessage("reservation.detail.panel.client"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
			GridBagConstraints gbc_panel = new GridBagConstraints();
			gbc_panel.fill = GridBagConstraints.HORIZONTAL;
			gbc_panel.insets = new Insets(0, 0, 5, 5);
			gbc_panel.gridx = 1;
			gbc_panel.gridy = 6;
			gbc_panel.gridwidth=8;
			getContentPane().add(panelPersonne, gbc_panel);
		}else{
			panelClient = new JPanel();
			panelClient.setBorder(new TitledBorder(null, MessagesUtil.formatMessage("reservation.detail.panel.client"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
			GridBagConstraints gbc_panelClient = new GridBagConstraints();
			gbc_panelClient.fill = GridBagConstraints.BOTH;
			gbc_panelClient.gridwidth = 2;
			gbc_panelClient.gridheight = 7; 
			gbc_panelClient.insets = new Insets(0, 0, 5, 0);
			gbc_panelClient.gridx = 1;
			gbc_panelClient.gridy = 6;
			getContentPane().add(panelClient, gbc_panelClient);
			GridBagLayout gbl_panelClient = new GridBagLayout();
			gbl_panelClient.columnWidths = new int[]{0, 0};
			gbl_panelClient.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
			gbl_panelClient.columnWeights = new double[]{0.5, 0.5};
			gbl_panelClient.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
			panelClient.setLayout(gbl_panelClient);

			
			JLabel Label_nom = new JLabel(MessagesUtil.formatMessage("reservation.detail.nom"));
			GridBagConstraints gbc_Label_nom = new GridBagConstraints();
			gbc_Label_nom.insets = new Insets(0, 0, 5, 5);
			gbc_Label_nom.anchor = GridBagConstraints.EAST;
			gbc_Label_nom.gridx = 0;
			gbc_Label_nom.gridy = 0;
			panelClient.add(Label_nom, gbc_Label_nom);
			
			textField_Nom = new JTextField();
			textField_Nom.setText(reservation.getClient().getNom());
			textField_Nom.setEditable(false);
			GridBagConstraints gbc_textField_Nom = new GridBagConstraints();
			gbc_textField_Nom.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_Nom.insets = new Insets(0, 0, 5, 5);
			gbc_textField_Nom.gridx = 1;
			gbc_textField_Nom.gridy = 0;
			panelClient.add(textField_Nom, gbc_textField_Nom);
			textField_Nom.setColumns(10);
			
			JLabel Label_prenom = new JLabel(MessagesUtil.formatMessage("reservation.detail.prenom"));
			GridBagConstraints gbc_Label_prenom = new GridBagConstraints();
			gbc_Label_prenom.insets = new Insets(0, 0, 5, 5);
			gbc_Label_prenom.anchor = GridBagConstraints.EAST;
			gbc_Label_prenom.gridx = 0;
			gbc_Label_prenom.gridy = 1;
			panelClient.add(Label_prenom, gbc_Label_prenom);
			
			textField_prenom = new JTextField(reservation.getClient().getPrenom());
			textField_prenom.setEditable(false);
			GridBagConstraints gbc_textField_prenom = new GridBagConstraints();
			gbc_textField_prenom.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_prenom.insets = new Insets(0, 0, 5, 5);
			gbc_textField_prenom.gridx = 1;
			gbc_textField_prenom.gridy = 1;
			panelClient.add(textField_prenom, gbc_textField_prenom);
			textField_prenom.setColumns(10);
			
			JLabel label = new JLabel(MessagesUtil.formatMessage("reservation.detail.ddn"));
			GridBagConstraints gbc_label = new GridBagConstraints();
			gbc_label.insets = new Insets(0, 0, 5, 5);
			gbc_label.anchor = GridBagConstraints.EAST;
			gbc_label.gridx = 0;
			gbc_label.gridy = 2;
			panelClient.add(label, gbc_label);
			
			textField_ddn = new JTextField(reservation.getClient().getDateNaissanceString());
			textField_ddn.setEditable(false);
			GridBagConstraints gbc_textField_ddn = new GridBagConstraints();
			gbc_textField_ddn.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_ddn.insets = new Insets(0, 0, 5, 5);
			gbc_textField_ddn.gridx = 1;
			gbc_textField_ddn.gridy = 2;
			panelClient.add(textField_ddn, gbc_textField_ddn);
			textField_ddn.setColumns(10);
			
			JLabel lblNewLabel_adresse = new JLabel(MessagesUtil.formatMessage("reservation.detail.adresse"));
			GridBagConstraints gbc_lblNewLabel_adresse = new GridBagConstraints();
			gbc_lblNewLabel_adresse.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_adresse.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_adresse.gridx = 0;
			gbc_lblNewLabel_adresse.gridy = 3;
			panelClient.add(lblNewLabel_adresse, gbc_lblNewLabel_adresse);
			
			textField_adresse = new JTextField(reservation.getClient().getAdresse());
			textField_adresse.setEditable(false);
			GridBagConstraints gbc_textField_adresse = new GridBagConstraints();
			gbc_textField_adresse.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_adresse.insets = new Insets(0, 0, 5, 5);
			gbc_textField_adresse.gridx = 1;
			gbc_textField_adresse.gridy = 3;
			panelClient.add(textField_adresse, gbc_textField_adresse);
			textField_adresse.setColumns(10);
			
			lblNewLabel_codePostal = new JLabel(MessagesUtil.formatMessage("reservation.detail.code.postal"));
			GridBagConstraints gbc_lblNewLabel_codePostal = new GridBagConstraints();
			gbc_lblNewLabel_codePostal.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_codePostal.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_codePostal.gridx = 0;
			gbc_lblNewLabel_codePostal.gridy = 4;
			panelClient.add(lblNewLabel_codePostal, gbc_lblNewLabel_codePostal);
			
			textField_codePostal = new JTextField(reservation.getClient().getCodePostal());
			textField_codePostal.setEditable(false);
			GridBagConstraints gbc_textField_codePostal = new GridBagConstraints();
			gbc_textField_codePostal.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_codePostal.insets = new Insets(0, 0, 5, 5);
			gbc_textField_codePostal.gridx = 1;
			gbc_textField_codePostal.gridy = 4;
			panelClient.add(textField_codePostal, gbc_textField_codePostal);
			textField_codePostal.setColumns(10);
			
			lblNewLabel_ville = new JLabel(MessagesUtil.formatMessage("reservation.detail.ville"));
			GridBagConstraints gbc_lblNewLabel_ville = new GridBagConstraints();
			gbc_lblNewLabel_ville.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_ville.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_ville.gridx = 0;
			gbc_lblNewLabel_ville.gridy = 5;
			panelClient.add(lblNewLabel_ville, gbc_lblNewLabel_ville);
			
			textField_ville = new JTextField(reservation.getClient().getVille());
			textField_ville.setEditable(false);
			GridBagConstraints gbc_textField_ville = new GridBagConstraints();
			gbc_textField_ville.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_ville.insets = new Insets(0, 0, 5, 5);
			gbc_textField_ville.gridx = 1;
			gbc_textField_ville.gridy = 5;
			panelClient.add(textField_ville, gbc_textField_ville);
			textField_ville.setColumns(10);
			
			lblNewLabel_telephone = new JLabel(MessagesUtil.formatMessage("reservation.detail.telephone"));
			GridBagConstraints gbc_lblNewLabel_telephone = new GridBagConstraints();
			gbc_lblNewLabel_telephone.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_telephone.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_telephone.gridx = 0;
			gbc_lblNewLabel_telephone.gridy = 6;
			panelClient.add(lblNewLabel_telephone, gbc_lblNewLabel_telephone);
			
			textField_telephone = new JTextField(reservation.getClient().getNumeroTelephone());
			textField_telephone.setEditable(false);
			GridBagConstraints gbc_textField_telephone = new GridBagConstraints();
			gbc_textField_telephone.insets = new Insets(0, 0, 5, 5);
			gbc_textField_telephone.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_telephone.gridx = 1;
			gbc_textField_telephone.gridy = 6;
			panelClient.add(textField_telephone, gbc_textField_telephone);
			textField_telephone.setColumns(10);
			
			panelEmplacement = new JPanel();
			panelEmplacement.setBorder(new TitledBorder(null, MessagesUtil.formatMessage("reservation.detail.panel.emplacement"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
			GridBagConstraints gbc_panelEmplacement = new GridBagConstraints();
			gbc_panelEmplacement.fill = GridBagConstraints.BOTH;
			gbc_panelEmplacement.gridwidth = 2;
			gbc_panelEmplacement.gridheight = 7; 
			gbc_panelEmplacement.insets = new Insets(0, 0, 5, 0);
			gbc_panelEmplacement.gridx = 3;
			gbc_panelEmplacement.gridy = 6;
			getContentPane().add(panelEmplacement, gbc_panelEmplacement);
			GridBagLayout gbl_panelEmplacement = new GridBagLayout();
			gbl_panelEmplacement.columnWidths = new int[]{0, 0};
			gbl_panelEmplacement.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
			gbl_panelEmplacement.columnWeights = new double[]{0.5, 0.5};
			gbl_panelEmplacement.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
			panelEmplacement.setLayout(gbl_panelEmplacement);
			
			lblNewLabel_emplacementNum = new JLabel(MessagesUtil.formatMessage("reservation.detail.emplacement"));
			GridBagConstraints gbc_lblNewLabel_emplacementNum = new GridBagConstraints();
			gbc_lblNewLabel_emplacementNum.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_emplacementNum.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_emplacementNum.gridx = 0;
			gbc_lblNewLabel_emplacementNum.gridy = 0;
			panelEmplacement.add(lblNewLabel_emplacementNum, gbc_lblNewLabel_emplacementNum);
			
			textField_emplacementNum = new JTextField(String.valueOf(reservation.getEmplacement().getId()));
			textField_emplacementNum.setEditable(false);
			GridBagConstraints gbc_textField_emplacement_num = new GridBagConstraints();
			gbc_textField_emplacement_num.insets = new Insets(0, 0, 5, 5);
			gbc_textField_emplacement_num.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_emplacement_num.gridx = 1;
			gbc_textField_emplacement_num.gridy = 0;
			panelEmplacement.add(textField_emplacementNum, gbc_textField_emplacement_num);	
			
			lblNewLabel_secteur = new JLabel(MessagesUtil.formatMessage("reservation.detail.secteur"));
			GridBagConstraints gbc_lblNewLabel_secteur = new GridBagConstraints();
			gbc_lblNewLabel_secteur.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_secteur.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_secteur.gridx = 0;
			gbc_lblNewLabel_secteur.gridy = 1;
			panelEmplacement.add(lblNewLabel_secteur, gbc_lblNewLabel_secteur);
				
			textField_emplacementSecteur = new JTextField(reservation.getEmplacement().getSecteur().getLibelle());
			textField_emplacementSecteur.setEditable(false);
			GridBagConstraints gbc_textField_emplacement_secteur = new GridBagConstraints();
			gbc_textField_emplacement_secteur.insets = new Insets(0, 0, 5, 5);
			gbc_textField_emplacement_secteur.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_emplacement_secteur.gridx = 1;
			gbc_textField_emplacement_secteur.gridy = 1;
			panelEmplacement.add(textField_emplacementSecteur, gbc_textField_emplacement_secteur);
			
			lblNewLabel_typeTerrain = new JLabel(MessagesUtil.formatMessage("reservation.detail.typeTerrain"));
			GridBagConstraints gbc_lblNewLabel_typeTerrain = new GridBagConstraints();
			gbc_lblNewLabel_typeTerrain.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_typeTerrain.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_typeTerrain.gridx = 0;
			gbc_lblNewLabel_typeTerrain.gridy = 2;
			panelEmplacement.add(lblNewLabel_typeTerrain, gbc_lblNewLabel_typeTerrain);
			
			textField_emplacementTypeTerrain = new JTextField(reservation.getEmplacement().getTypeTerrain().getLibelle());
			textField_emplacementTypeTerrain.setEditable(false);
			GridBagConstraints gbc_textField_emplacement_typeTerrain = new GridBagConstraints();
			gbc_textField_emplacement_typeTerrain.insets = new Insets(0, 0, 5, 5);
			gbc_textField_emplacement_typeTerrain.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_emplacement_typeTerrain.gridx = 1;
			gbc_textField_emplacement_typeTerrain.gridy = 2;
			panelEmplacement.add(textField_emplacementTypeTerrain, gbc_textField_emplacement_typeTerrain);
			
			
			panelCarte = new JPanel();
			panelCarte.setBorder(new TitledBorder(null, MessagesUtil.formatMessage("reservation.detail.panel.carte"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
			GridBagConstraints gbc_panelCarte = new GridBagConstraints();
			gbc_panelCarte.fill = GridBagConstraints.BOTH;
			gbc_panelCarte.gridwidth = 2;
			gbc_panelCarte.gridheight = 7; 
			gbc_panelCarte.insets = new Insets(0, 0, 5, 0);
			gbc_panelCarte.gridx = 5;
			gbc_panelCarte.gridy = 6;
			getContentPane().add(panelCarte, gbc_panelCarte);
			GridBagLayout gbl_panelCarte = new GridBagLayout();
			gbl_panelCarte.columnWidths = new int[]{0, 0};
			gbl_panelCarte.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
			gbl_panelCarte.columnWeights = new double[]{0.5, 0.5};
			gbl_panelCarte.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
			panelCarte.setLayout(gbl_panelCarte);
			
			lblNewLabel_Carte = new JLabel(MessagesUtil.formatMessage("reservation.detail.carte"));
			GridBagConstraints gbc_lblNewLabel_carte = new GridBagConstraints();
			gbc_lblNewLabel_carte.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_carte.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_carte.gridx = 0;
			gbc_lblNewLabel_carte.gridy = 0;
			panelCarte.add(lblNewLabel_Carte, gbc_lblNewLabel_carte);
			
			textField_carte = new JTextField();
			textField_carte.setEditable(false);
			GridBagConstraints gbc_textField_carte = new GridBagConstraints();
			gbc_textField_carte.insets = new Insets(0, 0, 5, 5);
			gbc_textField_carte.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_carte.gridx = 1;
			gbc_textField_carte.gridy = 0;
			panelCarte.add(textField_carte, gbc_textField_carte);
			
			lblNewLabel_badge = new JLabel(MessagesUtil.formatMessage("reservation.detail.badge"));
			GridBagConstraints gbc_lblNewLabel_badge = new GridBagConstraints();
			gbc_lblNewLabel_badge.insets = new Insets(0, 0, 5, 5);
			gbc_lblNewLabel_badge.anchor = GridBagConstraints.EAST;
			gbc_lblNewLabel_badge.gridx = 0;
			gbc_lblNewLabel_badge.gridy = 1;
			panelCarte.add(lblNewLabel_badge, gbc_lblNewLabel_badge);
			
			textField_badge = new JTextField();
			textField_badge.setEditable(false);
			GridBagConstraints gbc_textField_badge = new GridBagConstraints();
			gbc_textField_badge.insets = new Insets(0, 0, 5, 5);
			gbc_textField_badge.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_badge.gridx = 1;
			gbc_textField_badge.gridy = 1;
			panelCarte.add(textField_badge, gbc_textField_badge);	
			
			if(reservation.getStatut().getId() <= 2){
				btnAttributionCarte = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.arrivee"));
				btnAttributionCarte.addActionListener(this);
				if(reservation.getStatut().getId() != 2){
					btnAttributionCarte.setEnabled(false);
				}
				GridBagConstraints gbc_btnCarte = new GridBagConstraints();
				gbc_btnCarte.anchor = GridBagConstraints.EAST;
				gbc_btnCarte.insets = new Insets(0, 0, 5, 5);
				gbc_btnCarte.gridx = 1;
				gbc_btnCarte.gridy = 2;
				panelCarte.add(btnAttributionCarte, gbc_btnCarte);
			}else{
				btnRestitutionCarte = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.depart"));
				btnRestitutionCarte.addActionListener(this);
				if(reservation.getStatut().getId() != 3){
					btnRestitutionCarte.setEnabled(false);
				}
				GridBagConstraints gbc_btnCarte = new GridBagConstraints();
				gbc_btnCarte.anchor = GridBagConstraints.EAST;
				gbc_btnCarte.insets = new Insets(0, 0, 5, 5);
				gbc_btnCarte.gridx = 1;
				gbc_btnCarte.gridy = 2;
				panelCarte.add(btnRestitutionCarte, gbc_btnCarte);
			}
		}
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, MessagesUtil.formatMessage("reservation.detail.panel.financier"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridwidth = 6;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 16;
		getContentPane().add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0, 10, 0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, 0.15, 1.0, 1.0, 0.15, 0.05, 0.1, 1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
			
		JLabel lblFraisResa = new JLabel(MessagesUtil.formatMessage("reservation.detail.fraisResa"));
		GridBagConstraints gbc_lblFraisResa = new GridBagConstraints();
		gbc_lblFraisResa.anchor = GridBagConstraints.EAST;
		gbc_lblFraisResa.insets = new Insets(0, 0, 5, 5);
		gbc_lblFraisResa.gridx = 0;
		gbc_lblFraisResa.gridy = 0;
		panel.add(lblFraisResa, gbc_lblFraisResa);
		
		textFraisResa = new JTextField();
		GridBagConstraints gbc_textFraisResa = new GridBagConstraints();
		gbc_textFraisResa.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFraisResa.insets = new Insets(0, 0, 5, 5);
		gbc_textFraisResa.gridx = 1;
		gbc_textFraisResa.gridy = 0;
		panel.add(textFraisResa, gbc_textFraisResa);
		textFraisResa.setColumns(10);
		
		JLabel lblARRHES = new JLabel(MessagesUtil.formatMessage("reservation.detail.arrhes"));
		GridBagConstraints gbc_lblARRHES = new GridBagConstraints();
		gbc_lblARRHES.anchor = GridBagConstraints.EAST;
		gbc_lblARRHES.insets = new Insets(0, 0, 5, 5);
		gbc_lblARRHES.gridx = 2;
		gbc_lblARRHES.gridy = 0;
		panel.add(lblARRHES, gbc_lblARRHES);
		
		textARRHES = new JTextField();
		GridBagConstraints gbc_textARRHES = new GridBagConstraints();
		gbc_textARRHES.fill = GridBagConstraints.HORIZONTAL;
		gbc_textARRHES.insets = new Insets(0, 0, 5, 5);
		gbc_textARRHES.gridx = 3;
		gbc_textARRHES.gridy = 0;
		panel.add(textARRHES, gbc_textARRHES);
		textARRHES.setText("");
		textARRHES.setColumns(10);
		
		JLabel lblTotal;
		
		if(reservation.getStatut().getId() <= 1){
			lblTotal= new JLabel(MessagesUtil.formatMessage("reservation.detail.total"));
		}else{
			lblTotal= new JLabel(MessagesUtil.formatMessage("reservation.detail.resteApayer"));
		}
		
		GridBagConstraints gbc_lblTotal = new GridBagConstraints();
		gbc_lblTotal.anchor = GridBagConstraints.EAST;
		gbc_lblTotal.insets = new Insets(0, 0, 5, 5);
		gbc_lblTotal.gridx = 6;
		gbc_lblTotal.gridy = 0;
		panel.add(lblTotal, gbc_lblTotal);
		
		textTotal = new JTextField(String.valueOf(reservation.getMontant()));
		GridBagConstraints gbc_textTotal = new GridBagConstraints();
		gbc_textTotal.fill = GridBagConstraints.HORIZONTAL;
		gbc_textTotal.insets = new Insets(0, 0, 5, 0);
		gbc_textTotal.gridx = 7;
		gbc_textTotal.gridy = 0;
		panel.add(textTotal, gbc_textTotal);
		textTotal.setEnabled(false);
		textTotal.setText("");
		textTotal.setColumns(10);
		
		
		btnCalculMontant = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.calculer"));
		GridBagConstraints gbc_btnCalculMontant = new GridBagConstraints();
		gbc_btnCalculMontant.anchor = GridBagConstraints.EAST;
		gbc_btnCalculMontant.gridx = 7;
		gbc_btnCalculMontant.gridy = 1;
		panel.add(btnCalculMontant, gbc_btnCalculMontant);
		btnCalculMontant.addActionListener(this);
		if(reservation.getStatut().getId() >= 2){
			textFraisResa.setEnabled(false);
		}
		if(reservation.getStatut().getId() >= 2){
			textARRHES.setEnabled(false);
		}
		if(reservation.getStatut().getId() >= 1){
			btnCalculMontant.setEnabled(false);
		}
			
		if(reservation.getStatut().getId() >= 1){
			btnOccupant = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.occupant"));
			btnOccupant.addActionListener(this);
			GridBagConstraints gbc_btnOccupant = new GridBagConstraints();
			gbc_btnOccupant.insets = new Insets(0, 0, 5, 5);
			gbc_btnOccupant.gridx = 1;
			gbc_btnOccupant.gridy = 18;
			getContentPane().add(btnOccupant, gbc_btnOccupant);
			
			btnVisiteur = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.visiteur"));
			btnVisiteur.addActionListener(this);
			if(reservation.getStatut().getId() < 3){
				btnVisiteur.setEnabled(false);
			}			
			GridBagConstraints gbc_btnVisiteur = new GridBagConstraints();
			gbc_btnVisiteur.insets = new Insets(0, 0, 5, 5);
			gbc_btnVisiteur.gridx = 3;
			gbc_btnVisiteur.gridy = 18;
			getContentPane().add(btnVisiteur, gbc_btnVisiteur);
			
			btnFacture = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.facture"));
			btnFacture.addActionListener(this);
			if(reservation.getStatut().getId() < 3){
				btnFacture.setEnabled(false);
			}			
			GridBagConstraints gbc_btnFacture = new GridBagConstraints();
			gbc_btnFacture.insets = new Insets(0, 0, 5, 5);
			gbc_btnFacture.gridx = 6;
			gbc_btnFacture.gridy = 18;
			getContentPane().add(btnFacture, gbc_btnFacture);			
		
		}
		
		
		
		lblStatut = new JLabel(MessagesUtil.formatMessage("reservation.detail.statut"));
		GridBagConstraints gbc_lblStatut = new GridBagConstraints();
		gbc_lblStatut.anchor = GridBagConstraints.EAST;
		gbc_lblStatut.insets = new Insets(0, 0, 0, 5);
		gbc_lblStatut.gridx = 1;
		gbc_lblStatut.gridy = 19;
		getContentPane().add(lblStatut, gbc_lblStatut);
		
		textStatut = new JTextField(reservation.getStatut().getLibelle());
		textStatut.setEditable(false);
		GridBagConstraints gbc_textStatut = new GridBagConstraints();
		gbc_textStatut.insets = new Insets(0, 0, 0, 5);
		gbc_textStatut.fill = GridBagConstraints.HORIZONTAL;
		gbc_textStatut.gridx = 2;
		gbc_textStatut.gridy = 19;
		getContentPane().add(textStatut, gbc_textStatut);
		textStatut.setColumns(10);
		
		if(reservation.getStatut().getId() < 2){
			btnEnregistrer = new JButton(MessagesUtil.formatMessage("reservation.detail.btn.enregistrer"));
			btnEnregistrer.addActionListener(this);
			GridBagConstraints gbc_btnEnregistrer = new GridBagConstraints();
			gbc_btnEnregistrer.gridx = 6;
			gbc_btnEnregistrer.gridy = 19;
			getContentPane().add(btnEnregistrer, gbc_btnEnregistrer);
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
			
		if(reservation.getStatut().getId()==0){
			reservation.setClient(panelPersonne.getSelectedPersonne());
			reservation.setTypeLocation(listTypeLocation.get(comboBoxTypeLocation.getSelectedIndex()));
			reservation.setDateDebut(DateFormatUtils.parseDate(textDateDepart.getText()));
			reservation.setDateFin(DateFormatUtils.parseDate(textDateFin.getText()));
		}		
		
		if(e.getSource() == btnEnregistrer){	
			double arrhes = textARRHES.getText() == null || textARRHES.getText().equals("") ? 0.0 : Double.valueOf(textARRHES.getText());
			double fraisResa = textFraisResa.getText() ==null || textFraisResa.getText().equals("")? 0.0 : Double.valueOf(textFraisResa.getText());
			
			if(reservation.getClient() == null){
				ViewErreur frame = new ViewErreur(MessagesUtil.formatMessage("erreur.selection.client"));
				frame.setVisible(true);
			}else{
				this.controller.enregistrer(reservation, arrhes, fraisResa);
			}
			
		}else if(e.getSource() == btnCalculMontant){
			controller.calculer(reservation); 
		}else if(e.getSource() == btnTestDispo){
			controller.testDisponibilite(reservation);
		}else if(e.getSource() == btnOccupant){
			controllerOccupant = new OccupantController(reservation); 
			controllerOccupant.getViewDetail();
		}else if(e.getSource() == btnVisiteur){
			controllerVisiteur = new VisiteurController(reservation); 
			controllerVisiteur.getViewDetail();
		}else if(e.getSource() == btnAttributionCarte){
			controller.attributionCarte(reservation);
		}else if(e.getSource() == btnRetourListe ){
			this.dispose();
			controller.retour();
		}else if(e.getSource() == btnFacture){
			 controller.editionFacture(reservation);
		}else if(e.getSource() == btnRestitutionCarte){
			controller.restitutionCarte(reservation);
		}
		
	}
	
	public void update() {
		
		this.getContentPane().removeAll();
		
		initCompoment();
		
		if(reservation.getStatut().getId() == 0){
			//Mise à jour de la liste déroulante
		    //Supprime tout les anciens éléments
			comboBoxTypeLocation.removeAllItems();
			
			// On insère ensuite les nouvelles valeurs
			for(TypeLocation typeLocation : listTypeLocation){
				comboBoxTypeLocation.addItem(typeLocation.getLibelle());
			}
		}
		
		textTotal.setText(String.valueOf(reservation.getMontant()));
		
		textStatut.setText(reservation.getStatut().getLibelle());
		
		if(reservation.getStatut().getId() >= 3){
			textField_badge.setText(String.valueOf(reservation.getBadge().getId()));
		
			textField_carte.setText(String.valueOf(reservation.getCarte().getId()));
		}
		
		this.repaint();
		
	}

	public Reservation getReservation() {
		return reservation;
	}

	public void setReservation(Reservation reservation) {
		this.reservation = reservation;
		update();
	}
	
	
	public List<TypeLocation> getListTypeLocation() {
		return listTypeLocation;
	}

	public void setListTypeLocation(List<TypeLocation> listTypeLocation) {
		this.listTypeLocation = listTypeLocation;
		update();
	}

}
